'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken');
var svgCaptcha = require('svg-captcha');

class UserController extends Controller {
    //登录
    async login() {
        let {ctx,service} = this;
        let {username,password} = ctx.request.body;

        if(/[a-zA-Z0-9]{4,8}/.test(username) && password){
            //按用户名查是否能查到此人  1.如果能查到，用户名没有问题，密码有问题  2.没有查到，用户名有问题
            let target = await service.user.login(username,password);
            if(target.length){
                let token = jwt.sign({username},'lixd',{expiresIn:60*60*6});
                ctx.body = {
                    code:1,
                    msg:'登录成功',
                    token,
                    uuid:target[0].uuid
                }
            }
            
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //添加用户
    async add (){
        let {ctx,service} = this;

        let {hot,address,phone,tid,nickname} = ctx.request.body;

        if(hot&&address&&phone&&tid&&nickname){
            //添加用户
            //1.是否存在  2.添加  
            let target = await service.user.selectUser(nickname);
            if(target.length){
                //已经存在
                ctx.body = {
                    code:4,
                    msg:'此人已存在'
                }
            }else{
                //不存在
                try{
                    await service.user.add(hot,address,phone,tid,nickname);
                    ctx.body = {
                        code:1,
                        msg:'添加成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:'添加失败'
                    }
                }
            }
        }else{
            ctx.body ={
                code:2,
                msg:'参数有误'
            }
        }
    }

    //查询
    async list(){
        let {ctx,service} = this;
        let list = await service.user.list();
        ctx.body = {
            code:1,
            data:list
        }
    } 
    //查询用户类型
    async types(){
        let {ctx,service} = this;
        let types = await service.user.types();
        ctx.body = {
            code:1,
            data:types
        }
    }
    //查询此人的用户信息
    async info(){
        let {ctx,service} = this;
        let {uuid} = ctx.request.query;

        if(uuid){
            let target = await service.user.info(uuid);
            ctx.body = {
                code:1,
                data:target[0]
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //获取验证码
    async yan(){
        let {ctx} = this;
        var captcha = svgCaptcha.create();
        ctx.session.text = captcha.text;
        ctx.body = {
            code:1,
            data:captcha
        }
    }
}

module.exports = UserController;
